/*
 * @lc app=leetcode.cn id=637 lang=javascript
 *
 * [637] 二叉树的层平均值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var averageOfLevels = function (root) {
  let res = []
  let queque = []
  if (root === null) {
    return res
  }
  queque.push(root)
  while (queque.length) {
    let curLevelTotal = 0
    let curLevelLen=0
    const len=queque.length
    for (let i = 0; i < len; i++){
      const curNode = queque.shift()
      curLevelTotal += curNode.val
      curLevelLen++
      curNode.left&&queque.push(curNode.left)
      curNode.right&&queque.push(curNode.right)
    }
    const averageNum=curLevelTotal/curLevelLen
    res.push(averageNum)
  }
  return res

};
// @lc code=end

